package org.chunchu.todo;

import static org.chunchu.todo.Main.ACTION_ADD;

import javax.inject.Inject;

import org.chunchu.todo.dao.TaskDao;
import org.chunchu.todo.model.Task;
import org.chunchu.todo.mvp.impl.AddTask;
import org.chunchu.todo.mvp.impl.AddTaskPresenter;

import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectView;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

@ContentView(R.layout.taskadd)
public class AddTaskActivity extends RoboActivity implements AddTask {

	@InjectView(R.id.btnOkTask)
	private Button btnOk;
	@InjectView(R.id.btnCancelTask)
	private Button btnCancel;

	@InjectView(R.id.edtTaskName)
	private EditText edtName;
	@InjectView(R.id.edtTaskDetail)
	private EditText edtDetail;

	private String action = ACTION_ADD;

	@Inject
	TaskDao dao;
	@Inject
	private AddTaskPresenter presenter;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		btnOk.setOnClickListener(okListener);
		btnCancel.setOnClickListener(cancelListener);

		Intent intent = getIntent();
		action = intent.getAction();

		presenter.initializeWith(this);
	}

	@Override
	public String getAction() {
		return action;
	}

	@Override
	public void setTask(Task task) {
		task.applyTo(edtName, edtDetail);
	}

	@Override
	public void goToMain() {
		Intent intent = new Intent(this, Main.class);
		startActivity(intent);
	}

	private final OnClickListener okListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			presenter.saveTask(Task.from(edtName, edtDetail));
			finish();
		}
	};

	private final OnClickListener cancelListener = new View.OnClickListener() {
		@Override
		public void onClick(View v) {
			finish();
		}
	};

}
